<?php
namespace app\admin\controller;
use think\Db;

// 交办流程
class Processjb extends Authinfo {

	// 关联查询所需字段 hb_declare as a, hb_cadre_relative as cr
	private $gl_field = 'cr.id, i.id AS iid, cr.q_name AS name, cr.q_sfid AS sfid, cr.relationship, cr.name AS g_name, cr.sfid AS g_sfid, cr.job, cr.rank, i.name AS i_name, i.cat_name, i.company, i.amount, i.address, i.release_time';

	// 待交办列表
	public function getdjblist() {
		$order = input('order');					// 排序
		if (empty($order)) $order = 'cr.id desc';
		$current = input('current');						// 当前页
		if (!$current = intval($current)) $current = 1;
		$pagenums = input('pagenums');						// 每页显示数据
		if (!$pagenums = intval($pagenums)) $pagenums = 50;

		// 分页
		$start = ($current-1) * $pagenums;
		$map = [];

		$map[] = ['i.status','=',0];

		$truename = input('truename');
		if ($truename) $map[] = ['cr.q_name','like','%'.$truename.'%'];
		$sfid = input('sfid');
		if ($sfid) $map[] = ['cr.q_sfid','like','%'.$sfid.'%'];

		$total = Db::name('cadre_relative cr')->join('hb_income i','cr.q_sfid = i.sfid')->where($map)->count('cr.id');

		$list = Db::name('cadre_relative cr')
		->join('hb_income i','cr.q_sfid = i.sfid')
		->where($map)
		->order($order)->limit($start,$pagenums)->field($this->gl_field)
		->select();

		foreach ($list as &$value) {
			$value['is_shangbao'] = $this->bidui($value) ? '是' : '否';
		}

		return json(['status'=>1,'total'=>$total,'info'=>$list]);
	}

	/**
	 * income 表数据比对 其他两个表
	 * @author mxelle <demon_1926@163.com> 2018-08-15
	 * @param  array  $income 数据
	 * @return 比对结果，成功返回申报表id，失败返回false
	 */
	private function bidui2($income=[]) {
		if (empty($income)) return false;

		$cadre_relative = Db::name('cadre_relative')->where([
			'q_name' => $income['name'],
			'q_sfid' => $income['sfid'],
			])->find();
		if (!$cadre_relative) return false;
		// 比对申报表
		$declare = Db::name('declare')->where([
			's_name' => $income['name'],
			'q_sfid' => $income['sfid'],
			'company' => $income['company'],
			'cat_name' => $income['cat_name'],
			'price' => $income['amount'],
			'address' => $income['address'],
			'release_time' => $income['release_time'],
			])->find();
		if (!$declare) return false;
		return $declare['id'];
	}

	// 发送告知书 进入补录阶段
	public function sendgzs() {
		$ids = input('ids');
		// 上报ids
		if (empty($ids)) return json(['status'=>0,'info'=>'id丢失']);
		// 排除部分已经交办的
		$list = Db::name('income')->where('status','=',0)->whereIn('id',$ids)->select();
		if (empty($list)) return json(['status'=>0,'info'=>'数据丢失']);

		$qu = input('qu');
		$zhen = input('zhen');
		$cun = input('cun');
		$receiver = input('receiver');
		if (!$receiver) return json(['status'=>0,'info'=>'接收部门丢失']);
		$send = input('send');
		if (!$send) return json(['status'=>0,'info'=>'发送部门丢失']);
		$sendids = [];
		foreach ($list as $key => $value) {
			if (!$this->bidui2($value)) {
				Db::name('income')->where('id',$value['id'])->update(['status'=>2]);
				$sendids[] = $value['id'];
				// 比对失败的发送告知书
				$data = [
					'income_id' => $value['id'],
					'project' => $value['cat_name'],
					'qu' => $qu,
					'zhen' => $zhen,
					'cun' => $cun,
					'receiver' => $receiver,
					'send' => $send,
					'create_time' => time(),
					'create_id' => UID,
				];
				Db::name('notice_gzs')->insert($data);
			}
		}

		return json(['status'=>1,'info'=>'处理完毕!','ids'=>$sendids]);
	}

	// 发送核查通知书 进入核查流程
	public function sendtzs() {
		$ids = input('ids');
		// 上报ids
		if (empty($ids)) return json(['status'=>0,'info'=>'id丢失']);
		// 排除部分已经交办的
		$list = Db::name('income')->where('status','=',0)->whereIn('id',$ids)->select();
		if (empty($list)) return json(['status'=>0,'info'=>'数据丢失']);

		$qu = input('qu');
		$zhen = input('zhen');
		$cun = input('cun');
		$receiver = input('receiver');
		if (!$receiver) return json(['status'=>0,'info'=>'接收部门丢失']);
		$send = input('send');
		if (!$send) return json(['status'=>0,'info'=>'发送部门丢失']);
		$sendids = [];
		foreach ($list as $key => $value) {
			if ($did = $this->bidui2($value)) {
				Db::name('income')->where('id',$value['id'])->update(['status'=>1]);
				$sendids[] = $value['id'];
				// 比对失败的发送告知书
				$data = [
					'income_id' => $value['id'],
					'project' => $value['cat_name'],
					'qu' => $qu,
					'zhen' => $zhen,
					'cun' => $cun,
					'receiver' => $receiver,
					'send' => $send,
					'create_time' => time(),
					'create_id' => UID,
				];
				Db::name('notice_tzs')->insert($data);
				// 并记录在流程内
				Db::name('process')->insert([
					'catid' => 2,
					'declare_id' => $did,
					'income_id' => $value['id'],
					'uid' => UID,
					'create_time' => time(),
					]);
			}
		}

		return json(['status'=>1,'info'=>'处理完毕!','ids'=>$sendids]);
	}

	/**
	 * 受益表、干部表、上报表进行对比
	 * @author mxelle <demon_1926@163.com> 2018-08-12
	 * @param  array 	上报关联干部 生成的列表
	 * @return boolean	true-比对成功; false-比对失败
	 */
	private function bidui($info) {
		// 姓名与受益表比对
		if ($info['i_name']!=$info['name']) return false;
		$map = [
			// 干部亲属表 
			'g_name' => $info['g_name'],
			'sfid' => $info['g_sfid'],
			's_name' => $info['name'],
			'q_sfid' => $info['sfid'],
			'relationship' => $info['relationship'],
			// 受益表
			'cat_name' => $info['cat_name'],
			'company' => $info['company'],
			'price' => $info['amount'],
			'release_time' => $info['release_time'],
			'address' => $info['address'],
		];
		$bidui = Db::name('declare')->where($map)->find();
		if (!$bidui) return false;

		return true;
	}

	// 已交办列表
	public function getyjblist() {
		$order = input('order');					// 排序
		if (empty($order)) $order = 'cr.id desc';
		$current = input('current');						// 当前页
		if (!$current = intval($current)) $current = 1;
		$pagenums = input('pagenums');						// 每页显示数据
		if (!$pagenums = intval($pagenums)) $pagenums = 50;

		// 分页
		$start = ($current-1) * $pagenums;
		$map = [];

		$map[] = ['i.status','=',1];

		$truename = input('truename');
		if ($truename) $map[] = ['cr.q_name','like','%'.$truename.'%'];
		$sfid = input('sfid');
		if ($sfid) $map[] = ['cr.q_sfid','like','%'.$sfid.'%'];

		$total = Db::name('cadre_relative cr')->join('hb_income i','cr.q_sfid = i.sfid')->where($map)->count('cr.id');

		$list = Db::name('cadre_relative cr')
		->join('hb_income i','cr.q_sfid = i.sfid')
		->where($map)
		->order($order)->limit($start,$pagenums)->field($this->gl_field)
		->select();

		foreach ($list as &$value) {
			$value['is_shangbao'] = $this->bidui($value) ? '是' : '否';
		}

		return json(['status'=>1,'total'=>$total,'info'=>$list]);
	}

	// 待补录列表
	public function getdbllist() {
		$order = input('order');					// 排序
		if (empty($order)) $order = 'cr.id desc';
		$current = input('current');						// 当前页
		if (!$current = intval($current)) $current = 1;
		$pagenums = input('pagenums');						// 每页显示数据
		if (!$pagenums = intval($pagenums)) $pagenums = 50;

		// 分页
		$start = ($current-1) * $pagenums;
		$map = [];

		$map[] = ['i.status','=',3];

		$truename = input('truename');
		if ($truename) $map[] = ['cr.q_name','like','%'.$truename.'%'];
		$sfid = input('sfid');
		if ($sfid) $map[] = ['cr.q_sfid','like','%'.$sfid.'%'];

		$total = Db::name('cadre_relative cr')->join('hb_income i','cr.q_sfid = i.sfid')->where($map)->count('cr.id');

		$list = Db::name('cadre_relative cr')
		->join('hb_income i','cr.q_sfid = i.sfid')
		->where($map)
		->order($order)->limit($start,$pagenums)->field($this->gl_field)
		->select();

		foreach ($list as &$value) {
			$value['is_shangbao'] = $this->bidui($value) ? '是' : '否';
		}

		return json(['status'=>1,'total'=>$total,'info'=>$list]);
	}

	public function bulu() {
		$id = input('id');
		$income = Db::name('income')->where([['id','=',$id],['status','=',2]])->find();
		if (!$income) return json(['status'=>0,'info'=>'无需补录']);
		$data['g_name'] = input('g_name');
		$data['sfid'] = input('g_sfid');
		$data['relationship'] = input('relationship');
		$data['s_name'] = input('name');
		$data['q_sfid'] = input('sfid');
		$data['is_poor'] = input('is_poor');				// 是否低保户
		$data['is_households'] = input('is_households');	// 是否建卡贫困户
		$data['cat_name'] = input('cat_name'); // 惠民项目
		$data['basis'] = input('basis'); // 文件依据
		$data['address'] = input('address'); // 项目实施地址
		$data['content'] = input('content'); // 项目实施内容
		$data['price'] = input('price'); // 财政资金
		$data['company'] = input('company'); // 发放单位
		$data['release_time'] = input('release_time'); // 实施时间
		if ($data['q_sfid'] != $income['sfid']) return json(['status'=>0,'info'=>'身份证号码不匹配']); 

		$res = Db::name('declare')->insert($data);
		if ($res) {
			// 修改 income 表状态
			Db::name('income')->where('id',$id)->update(['status'=>0]);
			return json(['status'=>1,'info'=>'操作成功']);
		} else {
			return json(['status'=>1,'info'=>'网络错误']);
		}
	}
}